/**
 * @Copyright: 2010 杭州海康威视系统技术有限公司
 * @address: http://www.hikvision.com
 * @ProjectName: CMS电力平台
 * @Description: 电力的内部基础版本
 */
package com.hlkj.baojin.utils.hik;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


@SuppressWarnings("unchecked")
public class CommonFun {
	
	/**
	 * 字符串转时间
	 * 
	 * @author chixueshun May 14, 2011 11:15:41 AM
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date toDate(String date, String... format) {
		if (date == null || "".equals(date)) {
			return null;
		}
		String f = null;
		if (format.length > 0) {
			f = format[0];
		} else {
			f = "yyyy-MM-dd HH:mm:ss";
		}
		DateFormat dateFormat = new SimpleDateFormat(f);
		try {
			return dateFormat.parse(date);
		} catch (ParseException e) {
			LogUtil.logWarn(CommonFun.class, e);
		}
		return null;
	}


	/**
	 * 字符串转时间
	 * 
	 * @author chixueshun May 14, 2011 11:15:28 AM
	 * @param date
	 * @param format
	 * @return
	 */
	public static Calendar toCalendar(String date, String... format) {
		Date d = toDate(date, format);
		if (d != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(d);
			return calendar;
		}
		return null;
	}
	
	public static Object parseObject(Class clazz, String s) throws Exception {
		try {
			if (ObjectUtils.isStringEmpty(s)) {
				return null;
			}
			if (Integer.class == clazz) {
				return Integer.parseInt(s);
			} else if (Short.class == clazz) {
				return Short.parseShort(s);
			} else if (Long.class == clazz) {
				return Long.parseLong(s);
			} else if (Double.class == clazz) {
				return Double.parseDouble(s);
			} else if (Float.class == clazz) {
				return Float.parseFloat(s);
			} else if (Boolean.class == clazz) {
				return Boolean.parseBoolean(s);
			} else if (Byte.class == clazz) {
				return Byte.parseByte(s);
			} else if (Calendar.class == clazz) {
				return toCalendar(s);
			} else if (Date.class == clazz) {
				return toCalendar(s);
			} else if (String.class == clazz) {
				return s;
			}
		} catch (Exception e) {
			throw new Exception("格式化失败");
		}

		throw new Exception("类型不支持");
	}
	public static void main(String args[]) {
		// String s = "啊啊啊哦哦哦";
		// String ss = encode(s, "UTF-8");
		// System.out.println(ss);
		// System.out.println(decode(ss, "UTF-8"));
	}

}
